package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.dto.PageDto;
import edu.whut.mall.admin.dto.ProductDto;
import edu.whut.mall.admin.service.IProductService;
import edu.whut.mall.common.vo.Page;
import edu.whut.mall.common.api.ResponseMap;
import edu.whut.mall.common.vo.ProductTypeVo;
import edu.whut.mall.common.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;


/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-07 16:15
 */
@Slf4j
@Api(tags = {"产品"}, description = "产品管理", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController {

    private final IProductService productService;

    @ApiOperation(value = "获取产品类型")
    @RequestMapping(value = "/getProductType", method = RequestMethod.POST)
    public Map<String, Object> getProductType(HttpServletRequest request, @RequestParam int id) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        ProductTypeVo productType = productService.getProductType(id);
        if (productType == null) {
            return map.putFailure("获取失败,值为空", 0);
        }
        return map.putValue(productType, "获取成功");
    }

    @ApiOperation(value = "获取单个产品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE,
            notes = "根据商品id获取商品详情")
    @RequestMapping(value = "/getProduct", method = RequestMethod.POST)
    public Map<String, Object> getProduct(HttpServletRequest request, @RequestParam int id) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        ProductVo product = productService.getProduct(id);
        if (product != null) {
            return map.putValue(product, "获取成功");
        } else {
            return map.putFailure("获取失败", 0);
        }
    }

    @ApiOperation(value = "获取产品列表")
    @RequestMapping(value = "/getAllProductList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getAllProductList(HttpServletRequest request, @RequestParam Integer merchantId, @RequestBody PageDto pageDto) {
        ResponseMap map = ResponseMap.getInstance();
        Page<ProductVo> page = productService.getAllProductList(pageDto, merchantId);
        return map.putPage(page, "获取成功");
    }


    @ApiOperation("创建商品")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(HttpServletRequest request,@RequestBody ProductDto productDto) {
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String s = parameterNames.nextElement();
            log.info("name:"+s+" value:"+request.getParameter(s));
        }
        ResponseMap map = ResponseMap.getInstance();
        int count = productService.createProduct(productDto);
        if (count > 0) {
            return map.putValue(count,"创建成功");
        } else {
            return map.putFailure("创建失败",0);
        }
    }

}

